﻿/*
 * Copyright © 2020, Wuxi Safover Intelligent Technology Co., Ltd. All rights reserved.
 * 
 * 无锡安永智能科技有限公司版权所有，未经许可，不得用于商业用途
 * 
 * Visit: https://www.safover.com/ 
 */

using System;
using System.Collections.Generic;
using System.IO;
using Safover.LidarCollector.Config;
using Safover.LidarCollector.Extensions;
using Safover.Lidar.DataContract;

namespace Safover.LidarCollector.Managers
{
    public class V2LidarPacketFileSaver : LidarPacketFileSaver
    {

        private const string timeFormat = "yyyy-MM-dd HH-mm-ss"; 
        private const string angleFormat = "000.0";

        public override void Save(LidarRawFrame frame)
        {
            var fullName = BuildPath(frame);

            Json.SerializeObject(frame, fullName, null, Newtonsoft.Json.Formatting.None);
        }

        private string BuildPath(LidarRawFrame frame)
        {
            var folders = new List<string>
            {
                // 第一级根存储文件夹名称，由用户给定
                RuntimeConfigManager.Instance.OthersConfig.DataRootFolder,
                // 第二级文件夹名称为站点缩写（需唯一指定一台设备）
                RuntimeConfigManager.Instance.OthersConfig.StationAbbreviation,
            };

            var utcPacketTime = TimeUtils.Timestamp2UtcTime(frame.UtcPeriodicScanStartTimestamp);
            var utcFrameTime = TimeUtils.Timestamp2UtcTime(frame.UtcStartTimestamp);

            // 第三级根存储文件夹名称
            string secondFolder, lastFolder, fileName;
            switch (frame.LidarScanMode)
            {
                case LidarScanMode.Horizontal:
                    {
                        secondFolder = "H-Scan";
                        lastFolder = $"H {utcPacketTime.ToString(timeFormat)} {frame.StartAngle.ToString(angleFormat)}";
                        fileName = $"{utcFrameTime.ToString(timeFormat)} {frame.HorizontalAngle.ToString(angleFormat)}.{fileExtension}";
                    }
                    break;
                case LidarScanMode.Vertical:
                    {
                        secondFolder = "V-Scan";
                        lastFolder = $"V {utcPacketTime.ToString(timeFormat)} {frame.StartAngle.ToString(angleFormat)}";
                        fileName = $"{utcFrameTime.ToString(timeFormat)} {frame.VerticalAngle.ToString(angleFormat)}.{fileExtension}";
                    }
                    break;
                case LidarScanMode.Underway:
                    {
                        secondFolder = "U-Scan";
                        lastFolder = $"U {utcPacketTime.ToString(timeFormat)} {frame.VerticalAngle.ToString(angleFormat)}";
                        fileName = $"{utcFrameTime.ToString(timeFormat)}.{fileExtension}";
                    }
                    break;
                case LidarScanMode.Fixed:
                    {
                        secondFolder = "F-Scan";
                        lastFolder = $"F {utcPacketTime:yyyy-MM}";
                        fileName = $"{utcFrameTime.ToString(timeFormat)} {frame.VerticalAngle.ToString(angleFormat)}.{fileExtension}";
                    }
                    break;
                default:
                    throw new NotSupportedException($"Failed to create file path for LidarScanMode={frame.LidarScanMode}.");
            }

            folders.Add(secondFolder);
            folders.Add(lastFolder);

            var path = Path.Combine(folders.ToArray());

            if (!Directory.Exists(path))
            {
                _ = Directory.CreateDirectory(path);
            }

            var fullName = Path.Combine(path, fileName);

            return fullName;
        }
    }
}


